#include <sys/ipc.h>
#include <sys/shm.h>
#include "counter.h"
#include "mutex.h"

int shm_id;
int mutex_id;
int * counter;

int create_counter()
{
  shm_id = shm_get(IPC_PRIVATE, sizeof(int), 0600);
  counter = (int *) shmat(shm_id, 0, 0);
  mutex_id = mutex_create()
}

int get_counter_value(int counter_id)
{
  mutex_lock(mutex_id);
  int result = *counter;
  mutex_unlock(mutex_id);  
  return counter;
}

void increase_counter(int counter_id)
{
  mutex_lock(mutex_id);
  (*counter)++;
  mutex_unlock(mutex_id);  
}

void deattach_counter()
{
  shmdt((void*)counter);
}

void destroy_counter()
{
  shmctl(counter_id, IPC_RMID, 0);
}

